/*
 * @Author: xym100111100 253274870@qq.com
 * @Date: 2024-03-19 10:23:53
 * @LastEditors: xym100111100 253274870@qq.com
 * @LastEditTime: 2024-08-09 12:21:22
 * @FilePath: \RuoYi-Vue3\src\utils\dict.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import useDictStore from '@/store/modules/dict'
import { getDicts, type DictObj  } from '@/api/system/dict/data'
export type DictMapObj = { [key: string]: DictObj[] | undefined };
/**
 * 获取字典数据
 */
export function useDict(...args:string[]) {

  const res = ref<DictMapObj>({});
  return (() => {
    args.forEach((dictType, index) => {
      res.value[dictType] = [];
      const dicts = useDictStore().getDict(dictType);
      if (dicts) {
        res.value[dictType] = dicts;
      } else {
        getDicts(dictType).then(response => {
          res.value[dictType] = response.data.map((p:DictObj) => ({ label: p.dictLabel, value: p.dictValue, elTagType: p.listClass, elTagClass: p.cssClass }))
          useDictStore().setDict(dictType, res.value[dictType]);
        })
      
      }
    })
    return toRefs(res.value);
  })()
}